package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.until.QiniuUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.DDishes;
import com.ruoyi.system.service.IDDishesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 菜品档案Controller
 *
 * @author tjm
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/dishes")
public class DDishesController extends BaseController
{
    @Autowired
    private IDDishesService dDishesService;

    @Resource
    private QiniuUploadUtil qiniuUploadUtil;

    /**
     * 查询菜品档案列表
     */
    @RequiresPermissions("system:dishes:list")
    @GetMapping("/list")
    public TableDataInfo list(DDishes dDishes)
    {
        startPage();
        List<DDishes> list = dDishesService.selectDDishesList(dDishes);
        return getDataTable(list);
    }

    /**
     * 导出菜品档案列表
     */
    @RequiresPermissions("system:dishes:export")
    @Log(title = "菜品档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DDishes dDishes)
    {
        List<DDishes> list = dDishesService.selectDDishesList(dDishes);
        ExcelUtil<DDishes> util = new ExcelUtil<DDishes>(DDishes.class);
        util.exportExcel(response, list, "菜品档案数据");
    }

    /**
     * 获取菜品档案详细信息
     */
    @RequiresPermissions("system:dishes:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dDishesService.selectDDishesById(id));
    }

    /**
     * 新增菜品档案
     */
    @RequiresPermissions("system:dishes:add")
    @Log(title = "菜品档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DDishes dDishes)
    {
        return toAjax(dDishesService.insertDDishes(dDishes));
    }

    /**
     * 修改菜品档案
     */
    @RequiresPermissions("system:dishes:edit")
    @Log(title = "菜品档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DDishes dDishes)
    {
        return toAjax(dDishesService.updateDDishes(dDishes));
    }

    /**
     * 删除菜品档案
     */
    @RequiresPermissions("system:dishes:remove")
    @Log(title = "菜品档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dDishesService.deleteDDishesByIds(ids));
    }


    /**
     * 价格排行
     * */
    @RequiresPermissions("system:dishes:priceRank")
    @RequestMapping("/priceRank")
    public TableDataInfo priceRank(){
        List<DDishes>dDishes=dDishesService.priceRank();
        return getDataTable(dDishes);
    }

    /**
     * 口味查询
     * */
    @RequiresPermissions("system:dishes:flavor")
    @GetMapping("/flavor")
    public AjaxResult flavor(){
        return AjaxResult.success(dDishesService.flavor());
    }



    /**
     * 类别查询
     * */
    @RequiresPermissions("system:dishes:category")
    @GetMapping("/category")
    public AjaxResult category(){
        return AjaxResult.success(dDishesService.category());
    }



    /**
     * 规格查询
     * */
    @RequiresPermissions("system:dishes:specification")
    @GetMapping("/specification")
    public AjaxResult specification(){
        return AjaxResult.success(dDishesService.specification());
    }


    /**
     * 图片上传
     */
    @RequiresPermissions("system:dishes:upload")
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        try {
            String url = qiniuUploadUtil.uploadFile(file);
            return AjaxResult.success("上传成功", url);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败：" + e.getMessage());
        }
    }

}
